package org.just.dusin.ooii.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseConnectionSQL {
	private String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private String DBURL = "jdbc:sqlserver://localhost:1433;DatabaseName=KJ";
	private String DBUSER = "sa";
	private String DBPASSWORD = "sa";
	private Connection conn = null;

	public DatabaseConnectionSQL(String host,String port,String dbName,String user,String pass) throws ClassNotFoundException, SQLException {            
                
		Class.forName(DBDRIVER);
                DBURL = "jdbc:sqlserver://"+host+":"+port+";DatabaseName="+dbName;
                DBUSER = user;
                DBPASSWORD = pass;
                this.conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);		
	}
        public DatabaseConnectionSQL()throws ClassNotFoundException, SQLException{
            Class.forName(DBDRIVER);
            this.conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
        }
	public Connection getConnection() throws SQLException {
                if(conn==null)
                    conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);		
		return this.conn;
	}

	public void close() {
		if (this.conn != null) {
			try {
				this.conn.close();
                                conn=null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
